package com.fourth.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fourth.dao.BannerDao;
import com.ljx.entity.Banner;
import com.ljx.entity.HeadNav;
import com.ljx.vo.Result;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：lmy
 * @Date：2023/3/1 11:51
 */
@RestController
@RequestMapping("home/api/banner")
public class ApiBannerController {

    @Autowired(required = false)
    private BannerDao bannerDao;

    @RequestMapping("rotationSearch")
    public Result rotationSearch(@RequestParam Integer currentPage, @RequestParam Integer pageSize,
                                 @RequestBody Banner banner){

        Page<Banner> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        if (banner.getTitle() != ""){
            wrapper.like("title",banner.getTitle());
        }
        if (banner.getIsDisable() != ""){
            wrapper.like("is_disable",banner.getIsDisable());
        }

        Page<Banner> bannerPage = bannerDao.selectPage(page, wrapper);
        return new Result(2000,"查询成功",bannerPage);

    }

    @GetMapping("changeStatus")
    public Result changeStatus(@RequestParam String id,@RequestParam String isDisable){
        Banner banner = new Banner();
        banner.setId(id);
        banner.setIsDisable(isDisable);
        int row = bannerDao.updateById(banner);
        if (row == 1){
            return new Result(2000,"修改成功");
        }
        return  new Result(5000,"修改失败");
    }

    @GetMapping("deleteBanner")
    public Result deleteBanner(@RequestParam String id){
        int row = bannerDao.deleteById(id);
        if (row == 1){
            return new Result(2000,"删除成功");
        }
        return  new Result(5000,"删除失败");
    }

    @PostMapping("addBanner")
    public Result addBanner(@RequestBody Banner banner){
        int row = bannerDao.insert(banner);
        if (row == 1){
            return new Result(2000,"添加成功");
        }
        return  new Result(5000,"添加失败");
    }

    @PostMapping("editBanner")
    public Result editBanner(@RequestBody Banner banner){
        int row = bannerDao.updateById(banner);
        if (row == 1){
            return new Result(2000,"添加成功");
        }
        return  new Result(5000,"添加失败");
    }
}
